package com.mycompany.myapp.service.impl;

import com.mycompany.myapp.service.CommentService;
import com.mycompany.myapp.domain.Comment;
import com.mycompany.myapp.repository.CommentRepository;
import com.mycompany.myapp.service.dto.CommentDTO;
import com.mycompany.myapp.service.mapper.CommentMapper;
import com.mycompany.myapp.web.rest.utils.ResultObj;
import com.mycompany.myapp.web.rest.utils.TypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing {@link Comment}.
 */
@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    private final Logger log = LoggerFactory.getLogger(CommentServiceImpl.class);

    private final CommentRepository commentRepository;

    private final CommentMapper commentMapper;

    public CommentServiceImpl(CommentRepository commentRepository, CommentMapper commentMapper) {
        this.commentRepository = commentRepository;
        this.commentMapper = commentMapper;
    }

    /**
     * Save a comment.
     *
     * @param commentDTO the entity to save.
     * @return the persisted entity.
     */
    @Override
    public CommentDTO save(CommentDTO commentDTO) {
        log.debug("Request to save Comment : {}", commentDTO);
        Comment comment = commentMapper.toEntity(commentDTO);
        comment = commentRepository.save(comment);
        return commentMapper.toDto(comment);
    }

    /**
     * Get all the comments.
     *
     * @return the list of entities.
     */
    @Override
    @Transactional(readOnly = true)
    public List<CommentDTO> findAll() {
        log.debug("Request to get all Comments");
        return commentRepository.findAll().stream()
            .map(commentMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }


    /**
     * Get one comment by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<CommentDTO> findOne(Long id) {
        log.debug("Request to get Comment : {}", id);
        return commentRepository.findById(id)
            .map(commentMapper::toDto);
    }

    /**
     * Delete the comment by id.
     *
     * @param id the id of the entity.
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Comment : {}", id);
        commentRepository.deleteById(id);
    }

    public ResultObj createComment(CommentDTO commentDTO){
        if(!TypeUtils.isEmpty(commentDTO)){
            save(commentDTO);
            return ResultObj.backInfo(true,200,"评价成功",null);
        }
        return ResultObj.backInfo(false,200,"评价失败",null);
    }

    public List<CommentDTO> findByCommodityId(Long commodityId){
        if(!TypeUtils.isEmpty(commodityId)){
            return commentRepository.findByCommodityId(commodityId).stream()
                .map(commentMapper::toDto)
                .collect(Collectors.toCollection(LinkedList::new));
        }
        return null;
    }

    public ResultObj deleteComments(List<Long> commentIds){
        if (!TypeUtils.isEmpty(commentIds)) {
            for(Long id : commentIds){
                delete(id);
            }
            return ResultObj.backInfo(true,200,"删除成功",null);
        }
        return ResultObj.backInfo(false,200,"删除失败",null);
    }

}
